using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CueAngleScripts : MonoBehaviour
{
    public EventTrigger AngleEventTrigger; //整体屏幕的触发器
    public Image TutoriseImage;

    public bool IsMouseDown; //这个标记代表的是：如果为true，则代表是拖动齿轮UI进行移动。如果为false，则代表当前是拖动屏幕进行移动

    [Header("灵敏度")]
    public float GearSensitivity;
    public float MouseSensitivityX;
    public float MouseSensitivityY;
    public float GearAnimatorSensitivity;

    [Header("初始位置")]
    private Vector2 LastPos;

    private Vector2 LastInputPos;

    private Camera camera;

    private float MoveAngle;
    
    private bool Direction;

    private float MoveImageAngle; //累计移动齿轮的距离，超过一定距离后播放一次音乐！

    private float UpTime;
    private bool IsUp;

    private void Awake()
    {
        camera = Camera.main;

        EventTrigger.Entry AnglePointerDownEntry = new EventTrigger.Entry();
        AnglePointerDownEntry.eventID = EventTriggerType.PointerDown;
        AnglePointerDownEntry.callback.AddListener((data) => { OnAnglePointerDown((PointerEventData)data); });
        AngleEventTrigger.triggers.Add(AnglePointerDownEntry);


        EventTrigger.Entry AnglePointerUpEntry = new EventTrigger.Entry();
        AnglePointerUpEntry.eventID = EventTriggerType.PointerUp;
        AnglePointerUpEntry.callback.AddListener((data) => { OnAnglePointerUp((PointerEventData)data); });
        AngleEventTrigger.triggers.Add(AnglePointerUpEntry);

        EventTrigger.Entry AngleDragEntry = new EventTrigger.Entry();
        AngleDragEntry.eventID = EventTriggerType.Drag;
        AngleDragEntry.callback.AddListener((data) => { OnAngleDrag((PointerEventData)data); });
        AngleEventTrigger.triggers.Add(AngleDragEntry);

    }

    void Update()
    {
        if (IsUp)
        {
            UpTime += Time.deltaTime;
            if (UpTime >= 2)
            {
                UpTime = 0;
                IsUp = false;
                TutoriseImage.gameObject.SetActive(true);
            }
        }
    }

    private void OnAnglePointerDown(PointerEventData data)
    {
        TutoriseImage.gameObject.SetActive(false);
    }

    private void OnAnglePointerUp(PointerEventData data)
    {
        LastInputPos = Vector2.zero;
        MoveAngle = 0;
        IsUp = true;
    }

    private void OnAngleDrag(PointerEventData data)
    {
        if (LastInputPos == Vector2.zero)
        {
            LastInputPos = data.position;
        }
        Vector2 InputPosition = data.position;
        Vector3 screenPosition = camera.WorldToScreenPoint(BiliardGameManager.instance.whiteBall.transform.position);
        float m_Xangle = 0;
        float m_Yangle = 0;
        if (InputPosition.y > screenPosition.y)
        {
            m_Xangle = (InputPosition.x - LastInputPos.x) * MouseSensitivityX;
                BiliardGameManager.instance.CueRotGameObject.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Xangle);
                camera.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Xangle);
                MoveAngle += m_Xangle;
        }
        else
        {
            m_Xangle = (LastInputPos.x - InputPosition.x) * MouseSensitivityX;
                BiliardGameManager.instance.CueRotGameObject.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Xangle);
                camera.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Xangle);
                MoveAngle += m_Xangle;
        }

        if (InputPosition.x > screenPosition.x)
        {
            m_Yangle = (LastInputPos.y - InputPosition.y) * MouseSensitivityY;
                BiliardGameManager.instance.CueRotGameObject.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Yangle);
                camera.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Yangle);
                MoveAngle += m_Yangle;
        }
        else
        {
            m_Yangle = (InputPosition.y - LastInputPos.y) * MouseSensitivityY;
                BiliardGameManager.instance.CueRotGameObject.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Yangle);
                camera.transform.RotateAround(BiliardGameManager.instance.whiteBall.transform.position
                    ,Vector3.up,m_Yangle);
                MoveAngle += m_Yangle;
        }
        LastInputPos = Input.mousePosition;
    }
    private void OnCuePointerUp(PointerEventData data){
        LastPos = Vector2.zero;
        LastPos = Vector2.zero;
        MoveAngle = 0;
    }
}
